package com.huarui.partner.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import com.huarui.partner.dao.IPartnerAddressDao;
import com.huarui.partner.model.PartnerAddress;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class PartnerAddressDaoImpl extends BaseDaoImpl implements
		IPartnerAddressDao {

	@Override
	public void batchInsert(final List<PartnerAddress> itemList) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			public Object doInSqlMapClient(SqlMapExecutor executor)
					throws SQLException {

				executor.startBatch();
				int batch = 0;
				for (PartnerAddress item : itemList) {
					executor.insert(namespace + ".create", item);
					batch++;
					if (batch == 500) {
						executor.executeBatch();
						batch = 0;
					}
				}
				executor.executeBatch();
				return null;
			}
		});
	}

	// for( PartnerAddress item : itemList){

	// this.getSqlMapClientTemplate().insert(namespace+".create",item);

	// }

	@SuppressWarnings("unchecked")
	@Override
	public List<PartnerAddress> getPartnerAddressByPartnerId(Integer partnerId) {
		// TODO Auto-generated method stub
		return getSqlMapClientTemplate().queryForList(
				namespace + ".getPartnerAddressByPartnerId", partnerId);
	}

	@Override
	public void batchUpdate(List<PartnerAddress> itemList) {
		// TODO Auto-generated method stub

		for (PartnerAddress item : itemList) {

			this.getSqlMapClientTemplate().update(namespace + ".update", item);

		}

	}

	@Override
	public void batchDelete(final List<PartnerAddress> itemList) {

		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			public Object doInSqlMapClient(SqlMapExecutor executor)
					throws SQLException {

				executor.startBatch();
				int batch = 0;
				for (PartnerAddress item : itemList) {
					Integer id = item.getId();
					executor.insert(namespace + ".delete", id);
					batch++;
					if (batch == 500) {
						executor.executeBatch();
						batch = 0;
					}
				}
				executor.executeBatch();
				return null;
			}
		});

		// for( PartnerAddress item : itemList){

		// this.deleteById(item.getId());

		// }

	}

	@Override
	public List<PartnerAddress> getPartnerAddressByCondition(
			Map<String, Object> map) {

		return (List<PartnerAddress>) getSqlMapClientTemplate().queryForList(
				namespace + ".getPartnerAddressByCondition", map);
	}

	@Override
	public Integer getPartnerAddressCountByCondition(Map<String, Object> map) {

		return (Integer) getSqlMapClientTemplate().queryForObject(
				namespace + ".getPartnerAddressCountByCondition", map);
	}

}
